package com.neusoft.neuiotms.sap;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.neusoft.bizcore.auth.common.bean.OrgTreeBean;
import com.neusoft.bizcore.auth.common.bean.UserBean;
import com.neusoft.bizcore.auth.dto.AuthOganizationDTO;
import com.neusoft.bizcore.auth.dto.AuthRoleDTO;
import com.neusoft.bizcore.auth.dto.AuthUserDTO;
import com.neusoft.bizcore.auth.dto.DingConfigDTO;
import com.neusoft.bizcore.auth.service.AuthOganizationService;
import com.neusoft.bizcore.auth.service.AuthRoleService;
import com.neusoft.bizcore.auth.service.AuthUserService;
import com.neusoft.bizcore.auth.service.DingDingService;
import com.neusoft.bizcore.web.bean.TreeBean;
import com.neusoft.bizcore.web.dto.result.ResultDTO;
import com.neusoft.bizcore.web.dto.result.ResultListDTO;
import com.neusoft.bizcore.web.support.Searchable;
import com.neusoft.bizcore.web.utils.JsonUtils;
import com.neusoft.neuiotms.micro.auth.AuthMicroService;

@Service
public class AuthMicroServiceImpl implements AuthMicroService {

    @Autowired
    private AuthUserService authUserService;
    @Autowired
    private AuthOganizationService authOganizationService;
    @Autowired
    private DingDingService dingDingService;
    @Autowired
    private AuthRoleService authRoleService;

    @Override
    public ResultDTO<UserBean> loadUserByUsername(final String username) {
        final UserBean userBean = this.authUserService.loadUserByUsername(username);
        return ResultDTO.success(userBean);
    }

    @Override
    public ResultListDTO<Map<String, Object>> userIndex(final Map<String, Object> queryMap) {
        final Searchable searchable = new Searchable();
        if (null != queryMap) {
            queryMap.entrySet().forEach(entry -> {
                searchable.put(entry.getKey().substring("s_".length()), entry.getValue());
            });
        }
        final List<AuthUserDTO> DTOs = this.authUserService.findAll(searchable);
        final List<Map<String, Object>> list =
                DTOs.stream().map(dto -> JsonUtils.pojoToMap(dto)).collect(Collectors.toList());
        return ResultListDTO.success(list);
    }

    @Override
    public ResultListDTO<Map<String, Object>> orgIndex() {
        final List<AuthOganizationDTO> DTOs = this.authOganizationService.findAll();
        final List<Map<String, Object>> list =
                DTOs.stream().map(dto -> JsonUtils.pojoToMap(dto)).collect(Collectors.toList());
        return ResultListDTO.success(list);
    }

    @Override
    public ResultListDTO<Map<String, Object>> batchusers(final Map<String, String[]> usernames) {
        final String[] mUsernames = usernames.get("usernames");
        if (null == mUsernames) {
            return ResultListDTO.success(new ArrayList<>());
        }

        final List<AuthUserDTO> DTOs = this.authUserService.details(Arrays.asList(mUsernames));
        final List<Map<String, Object>> list =
                DTOs.stream().map(dto -> JsonUtils.pojoToMap(dto)).collect(Collectors.toList());
        return ResultListDTO.success(list);
    }

    @Override
    public ResultListDTO<Map<String, Object>> allOrganizations() {
        final List<AuthOganizationDTO> DTOs = this.authOganizationService.findAll();
        final List<Map<String, Object>> list =
                DTOs.stream().map(dto -> JsonUtils.pojoToMap(AuthOganizationDTO.class)).collect(Collectors.toList());
        return ResultListDTO.success(list);
    }

    @Override
    public ResultListDTO<Map<String, Object>> allUsers(final Map<String, Object> queryMap) {
        final Searchable searchable = new Searchable();
        if (null != queryMap) {
            queryMap.entrySet().forEach(entry -> {
                searchable.put(entry.getKey().substring("s_".length()), entry.getValue());
            });
        }
        final List<AuthUserDTO> DTOs = this.authUserService.findAll(searchable);
        final List<Map<String, Object>> list =
                DTOs.stream().map(dto -> JsonUtils.pojoToMap(dto)).collect(Collectors.toList());
        return ResultListDTO.success(list);
    }

    @Override
    public ResultDTO<TreeBean> organizationTree() {
        final OrgTreeBean tree = this.authOganizationService.orgTree("root");
        final String json = JsonUtils.pojoToJson(tree);
        final TreeBean treeBean = JsonUtils.jsonToPojo(json, TreeBean.class);
        return ResultDTO.success(treeBean);

    }

    @Override
    public ResultDTO<Map<String, Object>> roleByCode(final String code) {
        final AuthRoleDTO dto = this.authRoleService.detailsByCode(code);
        if (null != dto) {
            final String json = JsonUtils.pojoToJson(dto);
            final Map<String, Object> map = JsonUtils.jsonToMap(json);
            return ResultDTO.success(map);
        }
        return ResultDTO.failure(null);
    }

    @Override
    public ResultDTO<Map<String, Object>> getDingDingConfig() {
        final DingConfigDTO dto = this.dingDingService.getDingConfig();
        if (null != dto) {
            final String json = JsonUtils.pojoToJson(dto);
            final Map<String, Object> map = JsonUtils.jsonToMap(json);
            return ResultDTO.success(map);
        }
        return ResultDTO.failure(null);
    }

    @Override
    public ResultDTO<String> exchangeDingUser(final String username) {
        final String dingUserId = this.dingDingService.getDingUserIdBySystemUser(username);
        return ResultDTO.success(dingUserId);
    }

}
